package com.xinhe.web.selfpay;


import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;

import java.net.URL;

/** JSAPI 下单为例 */
public class QuickStart {
     /*我的信息
        userid：604183377410523136
        openid：olYZb5aMoVTziZtD8MYK5dIV8TmE
        unionid：oaniQ6U5MUVuwINMyxdA-K4f8yWg
        configid：519802482830868480
        appid：wx2425d0af98810b00
        appsecret：f74746117f13a69ef272055921e81b3f
        mchid:1616713201
        paykey:af42f8b7ecc71d8995b8f98bd6baad28
        证书序列号：57906AE4C493222D0DDD03D81A3A2F2AA93B8D19
        apiV3Key：12345678901234567890ABCDEFGHIJKL
        */

    /** 商户号 */
    public static String merchantId = "1616713201";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "";
    /** 商户证书序列号 */
    public static String merchantSerialNumber = "57906AE4C493222D0DDD03D81A3A2F2AA93B8D19";
    /** 商户APIV3密钥 */
    public static String apiV3Key = "12345678901234567890ABCDEFGHIJKL";

    public static void main(String[] args) {
        URL resource = AppTest.class.getClassLoader().getResource("cert/apiclient_key.pem");
        if (resource != null) {
            privateKeyPath = resource.getPath();
            System.out.println("文件路径：" + privateKeyPath);
        } else {
            System.out.println("找不到指定的文件");
        }
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        JsapiService service = new JsapiService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid("wx2425d0af98810b00");
        request.setMchid("1616713201");
        request.setDescription("测试商品标题");
        request.setNotifyUrl("http://1941w5190a.yicp.fun:49760/test/acceptWeixin");
        request.setOutTradeNo("out_trade_no_888");
        Payer payer = new Payer();
        payer.setOpenid("olYZb5aMoVTziZtD8MYK5dIV8TmE");
        request.setPayer(payer);
        PrepayResponse response = service.prepay(request);
        System.out.println("哈哈哈："+JSONObject.toJSONString(response));
        System.out.println(response.getPrepayId());
        //{"prepay_id":"wx03210827142344537cd8e92f5819330000"}
    }
}